{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 01\n",
    "\n",
    "# 矩阵加法，向量化运算\n",
    "Book_3《数学要素》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "272ea6e0-6024-4f46-a4c6-7b4b678e0ca0",
   "metadata": {},
   "source": [
    "该代码使用`numpy`库对两个矩阵$A$和$B$进行逐元素相加，并展示了两种方法：直接转换为`numpy`数组相加和使用`numpy`的`add`函数。矩阵逐元素相加是指对于两个大小相同的矩阵，将它们的对应位置的元素分别相加，形成新的矩阵。\n",
    "\n",
    "### 代码细节\n",
    "\n",
    "1. **定义矩阵**：代码定义了两个矩阵$A$和$B$，矩阵$A$为：\n",
    "\n",
    "   $$\n",
    "   A = \\begin{bmatrix} 1 & 2 & 3 \\\\ 4 & 5 & 6 \\end{bmatrix}\n",
    "   $$\n",
    "\n",
    "   矩阵$B$为：\n",
    "\n",
    "   $$\n",
    "   B = \\begin{bmatrix} 1 & 0 & 0 \\\\ 0 & 1 & 0 \\end{bmatrix}\n",
    "   $$\n",
    "\n",
    "2. **逐元素相加**：\n",
    "   - 使用`np.array(A) + np.array(B)`：首先将$A$和$B$转换为`numpy`数组，再进行逐元素相加。结果为：\n",
    "\n",
    "     $$\n",
    "     A + B = \\begin{bmatrix} 2 & 2 & 3 \\\\ 4 & 6 & 6 \\end{bmatrix}\n",
    "     $$\n",
    "\n",
    "   - 使用`np.add(A, B)`：`numpy`的`add`函数用于逐元素相加，得到与前一种方法相同的结果。\n",
    "\n",
    "### 总结\n",
    "\n",
    "这段代码展示了在`numpy`中对矩阵进行逐元素相加的便捷性。对于矩阵$A$和$B$的对应位置上的每个元素$A[i][j]$和$B[i][j]$，计算它们的和，得到新的矩阵。这种逐元素相加方式在科学计算和数据处理中特别常见。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26f3d8be-6fbf-4d58-a731-1a487f0ac796",
   "metadata": {},
   "source": [
    "## 使用numpy对两个矩阵进行逐元素相加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "450fd65e-f818-4602-a5bd-f80100d34762",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np  # 导入numpy库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "dcf1b9d4-7b0e-46de-a8ea-bf9e78882849",
   "metadata": {},
   "outputs": [],
   "source": [
    "A = [[1, 2, 3],  # 定义矩阵A\n",
    "     [4, 5, 6]]\n",
    "# A = np.array(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6010c989-cdcc-4549-857d-45e058454c34",
   "metadata": {},
   "outputs": [],
   "source": [
    "B = [[1, 0, 0],  # 定义矩阵B\n",
    "     [0, 1, 0]]\n",
    "# B = np.array(B)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ba46565-0b12-4f47-950c-d85ffaa2b436",
   "metadata": {},
   "source": [
    "## 使用numpy的数组相加方法对矩阵A和矩阵B逐元素相加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3ff797b0-4c01-45cb-974c-3323b1b9e5bc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 2 3]\n",
      " [4 6 6]]\n"
     ]
    }
   ],
   "source": [
    "print(np.array(A) + np.array(B))  # 将A和B转换为numpy数组并逐元素相加"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7392c67-6e4a-4bb4-9eea-5bbcf73a2f0b",
   "metadata": {},
   "source": [
    "## 使用numpy的add函数对矩阵A和矩阵B逐元素相加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "12b2a460-1611-40bb-bd42-924c878838be",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 2 3]\n",
      " [4 6 6]]\n"
     ]
    }
   ],
   "source": [
    "print(np.add(A, B))  # 使用numpy的add函数逐元素相加并输出结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
